﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace CPPEI.Coolzon.UI.XProperty.Editors
{
    /// <summary>
    /// DataFilterEditWindow.xaml 的交互逻辑
    /// </summary>
    public partial class DataFilterEditWindow : Window
    {
        public DataFilter RetDataFilter = new DataFilter();

        private List<string> _dataList;
        private ObservableCollection<DataFilterModel> _modelList = new ObservableCollection<DataFilterModel>();

        public DataFilterEditWindow(DataFilter xDataFilter, List<string> dataResult)
        {
            InitializeComponent();
            if(xDataFilter != null)
            {
                foreach (var item in xDataFilter)
                {
                    RetDataFilter.Add(item);
                }
            }

            _dataList = dataResult;

            InitListView();
            ResetText();
            _tbText.KeyDown += _tbText_KeyDown;
            _tbText.LostFocus += _tbText_LostFocus;
        }

        private void InitListView()
        {
            _modelList.Clear();
            if (_dataList != null && _dataList.Count > 0)
            {
                foreach(var item in RetDataFilter)
                {
                    DataFilterModel model = new DataFilterModel();
                    model.Name = item;
                    model.IsChecked = true;
                    model.PropertyChanged += Model_PropertyChanged;
                    _modelList.Add(model);
                }
                foreach (var item in _dataList)
                {
                    if (!RetDataFilter.Contains(item))
                    {
                        DataFilterModel model = new DataFilterModel();
                        model.Name = item;
                        model.IsChecked = false;
                        model.PropertyChanged += Model_PropertyChanged;
                        _modelList.Add(model);
                    }
                }
            }
            _listView.ItemsSource = _modelList;
        }

        private void Model_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            ResetText();
        }

        private void ResetText()
        {
            RetDataFilter.Clear();
            foreach (var item in _modelList)
            {
                if (item.IsChecked)
                {
                    RetDataFilter.Add(item.Name);
                }
            }
            _tbText.Text = RetDataFilter.ToString();
        }

        private void ResetXDataFilterByText()
        {
            RetDataFilter.InitByText(_tbText.Text);
            InitListView();
        }


        private void _tbText_LostFocus(object sender, RoutedEventArgs e)
        {
            ResetXDataFilterByText();
        }

        private void _tbText_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                ResetXDataFilterByText();
            }
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = true;
        }

        private void cancel_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }

        private void _ctlUp_Click(object sender, RoutedEventArgs e)
        {
            if(_listView.SelectedItem != null && _listView.SelectedIndex > 0)
            {
                int selectedIndex = _listView.SelectedIndex;
                DataFilterModel model = _listView.SelectedItem as DataFilterModel;
                _modelList.RemoveAt(selectedIndex);
                _modelList.Insert(selectedIndex - 1, model);
                _listView.SelectedItem = model;
                ResetText();
            }
        }

        private void _ctlDown_Click(object sender, RoutedEventArgs e)
        {
            if (_listView.SelectedItem != null && _listView.SelectedIndex < _modelList.Count - 1)
            {
                int selectedIndex = _listView.SelectedIndex;
                DataFilterModel model = _listView.SelectedItem as DataFilterModel;
                _modelList.RemoveAt(selectedIndex);
                _modelList.Insert(selectedIndex + 1, model);
                _listView.SelectedItem = model;
                ResetText();
            }
        }
    }

    class DataFilterModel : INotifyPropertyChanged
    {
        private string _Name;
        public string Name
        {
            get
            {
                return _Name;
            }
            set
            {
                _Name = value;
                NotifyPropertyChanged();
            }
        }
        private bool _IsChecked;
        public bool IsChecked
        {
            get
            {
                return _IsChecked;
            }
            set
            {
                _IsChecked = value;
                NotifyPropertyChanged();
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        public void NotifyPropertyChanged([CallerMemberName]string propertyName = "")
        {
            if(PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
}
